package com.zzw.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzw.bean.Order;
import com.zzw.bean.OrderItem;
import com.zzw.bean.Product;
import com.zzw.dao.OrderMapper;
import com.zzw.service.OrderService;
import com.zzw.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

   /* @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductService productService;

    @Override
    @Transactional
    public int addOrder(Order order) {
        // 设置初始状态为待确认
        order.setStatus(0);
        // 初始化已发货数量为0
        order.setShippedQuantity(0);
        Date now = new Date();
        order.setCreateTime(now);
        order.setUpdateTime(now);

        // 如果没有订单号，则生成一个
        if (order.getOrderNo() == null || order.getOrderNo().isEmpty()) {
            order.setOrderNo(generateOrderNo());
        }

        // 计算订单总金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (OrderItem item : order.getOrderItems()) {
            item.setCreateTime(now);
            // 初始化已发货数量为0
            item.setShippedQuantity(0);

            // 从商品表获取最新产品信息
            if (item.getProductId() != null) {
                Product product = productService.getProductById(item.getProductId());
                if (product != null) {
                    // 更新商品信息
                    item.setProductName(product.getProductName());
                    // 如果前端没有传入价格，则使用商品表中的标准价格
                    if (item.getProductPrice() == null || item.getProductPrice().compareTo(BigDecimal.ZERO) == 0) {
                        item.setProductPrice(product.getPrice());
                    }
                }
            }

            // 计算小计金额
            if (item.getTotalPrice() == null || item.getTotalPrice().compareTo(BigDecimal.ZERO) == 0) {
                BigDecimal itemTotal = item.getProductPrice().multiply(new BigDecimal(item.getQuantity()));
                item.setTotalPrice(itemTotal);
            }
            totalAmount = totalAmount.add(item.getTotalPrice());
        }
        order.setTotalAmount(totalAmount);

        // 插入订单
        orderMapper.insertOrder(order);

        // 设置订单项的orderId
        for (OrderItem item : order.getOrderItems()) {
            item.setOrderId(order.getId());
        }

        // 批量插入订单项
        orderMapper.batchInsertOrderItems(order.getOrderItems());

        return order.getId();
    }

    @Override
    public int confirmOrder(Integer id) {
        return orderMapper.updateOrderStatus(id, 1);
    }

    @Override
    public int updateOrderToShipped(Integer id) {
        return orderMapper.updateOrderStatus(id, 2);
    }

    @Override
    public int completeOrder(Integer id) {
        return orderMapper.updateOrderStatus(id, 3);
    }

    @Override
    public Order getOrderById(Integer id) {
        return orderMapper.selectOrderById(id);
    }

    @Override
    public Order getOrderByOrderNo(String orderNo) {
        return orderMapper.selectOrderByOrderNo(orderNo);
    }

    @Override
    public Order getOrderDetailById(Integer id) {
        return orderMapper.selectOrderDetailById(id);
    }

    @Override
    public PageInfo<Order> getOrderList(Map<String, Object> params, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orders = orderMapper.selectOrderList(params);
        return new PageInfo<>(orders);
    }

    @Override
    @Transactional
    public int deleteOrder(Integer id) {
        // 先删除订单项
        orderMapper.deleteOrderItemsByOrderId(id);
        // 再删除订单
        return orderMapper.deleteOrder(id);
    }

    @Override
    public String generateOrderNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        String timeMillis = String.valueOf(System.currentTimeMillis()).substring(9);
        return "ORD" + dateStr + timeMillis;
    }

    @Override
    public int updateOrderItemShippedQuantity(Integer orderItemId, Integer shippedQuantity) {
        return orderMapper.updateOrderItemShippedQuantity(orderItemId, shippedQuantity);
    }

    @Override
    public int updateOrderShippedQuantity(Integer orderId, Integer shippedQuantity) {
        return orderMapper.updateOrderShippedQuantity(orderId, shippedQuantity);
    }

    @Override
    public OrderItem getOrderItemById(Integer orderItemId) {
        return orderMapper.selectOrderItemById(orderItemId);
    }*/
    @Resource
    private OrderMapper orderMapper;


    @Override
    public Order selectOrderById(Long id) {
        return orderMapper.selectOrderById(id);
    }

    @Override
    public Order selectOrderByOrderNo(String orderNo) {
        return orderMapper.selectOrderByOrderNo(orderNo);
    }

    @Override
    public List<Order> selectOrderList() {
        return orderMapper.selectOrderList();
    }

    @Override
    public int insertOrder(Order order) {
        return orderMapper.insertOrder(order);
    }

    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }
}
